<template>
    <div>
        <Bread></Bread>
        <el-card>
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-input placeholder="请输入内容" v-model="query.query" class="input-with-select">
                        <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="6">
                    <el-button type="primary" @click="add">添加用户</el-button>
                </el-col>
            </el-row>

            <el-table :data="userList" border style="width: 100%">
                <el-table-column type="index">
                </el-table-column>
                <el-table-column prop="username" label="姓名">
                </el-table-column>
                <el-table-column prop="email" label="邮箱">
                </el-table-column>
                <el-table-column prop="email" label="电话">
                </el-table-column>
                <el-table-column prop="role_name" label="角色">
                </el-table-column>
                <el-table-column prop="mg_state" label="状态">
                    <template slot-scope="scope">
                        <el-switch v-model="scope.row.mg_state" active-color="#13ce66" inactive-color="#ff4949"
                            @change="status(scope.row.id, scope.row.mg_state)">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column prop="address" label="操作" width="220px">
                    <template slot-scope="scope">
                        <el-button type="warning" size="mini" @click="editUser(scope.row.id)">编辑</el-button>
                        <el-button type="danger" size="mini" @click="delUser(scope.row.id)">删除</el-button>
                        <el-button type="success" size="mini">设置</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 对话框组件 -->
            <Dialog :myDialog="myDialog" :addUser="addUser" @Update="getUserList" :editId="editId"></Dialog>
            <!-- 分页 -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="query.pagenum" :page-sizes="[2, 3, 4, 5]" :page-size="query.pagesize"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
import Bread from '@/components/Bread.vue'
// import Dialog from '@/components/Dialog.vue'
import getUserList from '@/utils/userApi/userListApi'
import delUser from '@/utils/userApi/userDelApi'
import statusUser from '@/utils/userApi/userStatusApi'
export default {
    components: { Bread, Dialog },
    data() {
        return {
            userList: [],
            /* 用户列表参数 */
            query: {
                query: '',
                pagenum: 1,
                pagesize: 5
            },
            /* 添加用户参数 */
            addUser: {
                username: '',
                password: '',
                email: '',
                mobile: ''
            },
            /* 对话框基础数据 */
            myDialog: {
                type: '',
                flag: false,
                title: '',
            },
            /* 编辑id */
            editId: 1,
            total:0
        }
    },
    methods: {
        async getUserList() {
            /* 请求用户列表接口 */
            let res = await getUserList(this.query);
            this.userList = res.data.users;
            this.total = res.data.total;
        },
        /* 修改状态 */
        async status(flag, type) {
            // console.log(flag);
            let res = await statusUser(flag, type);
            if (res.meta.status == 200) {
                this.$message.success(res.meta.msg);
                this.getUserList();
            } else {
                this.$message.success(res.meta.msg);
            }
        },
        /* 添加用户 */
        add() {
            (
                this.myDialog = {
                    type: 'add',
                    flag: true,
                    title: '添加用户',
                }
            ),
                (
                    this.addUser = {
                        username: '',
                        password: '',
                        email: '',
                        mobile: ''
                    }
                )
            // alert('jjj')
        },
        /* 删除用户 */
        delUser(del) {
            // console.log(del);
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                /* 调用删除用户接口 */
                let res = await delUser(del);
                if (res.meta.status == 200) {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getUserList();
                }

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        /* 编辑用户 */
        editUser(edit) {
            console.log(edit);
            this.editId = edit;
            this.myDialog = {
                type: 'edit',
                flag: true,
                title: '编辑用户',
            }
        },
        /* 页码变化 */
        handleCurrentChange(num) {
            console.log(num);
            this.query.pagenum = num;
            this.getUserList();
        },
        /* 页数变化 */
        handleSizeChange(size) {
            console.log(size);
            this.query.pagesize = size;
            this.getUserList();
        }
    },
    mounted() {
        this.getUserList();
    }
}
</script>

<style lang="scss" scoped>

</style>